package gs.eisenhower.swallet.adapter;

import gs.eisenhower.swallet.R;
import gs.eisenhower.swallet.model.Wallet;
import gs.eisenhower.swallet.utils.constants.WalletConstants;

import java.text.NumberFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class WalletAdapter extends ArrayAdapter<Wallet> {
	private List<Wallet> wallets;
	private List<String> transactionDates;
	private Context context;

	public WalletAdapter(List<Wallet> wallets, List<String> transactionDates, Context context) {
	    super(context, R.layout.row_history_item, wallets);
	    this.wallets = wallets;
	    this.context = context;
	    this.transactionDates = transactionDates;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {		
	    if (convertView == null) {
	        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        convertView = inflater.inflate(R.layout.row_history_item, parent, false);
	    }
	        TextView type        = (TextView) convertView.findViewById(R.id.typeTextView);
	        TextView category    = (TextView) convertView.findViewById(R.id.categoryTextView);
	        TextView transaction = (TextView) convertView.findViewById(R.id.transactionDateTextView);
	        TextView details     = (TextView) convertView.findViewById(R.id.detailsTextView);
	        TextView amount      = (TextView) convertView.findViewById(R.id.amountTextView);
	        
	        Wallet wallet = wallets.get(position);
	        type.setText("Type: " + wallet.getType());
	        category.setText("Category: " + wallet.getCategory());
	        transaction.setText("Time: " + transactionDates.get(position));
	        details.setText("Details: " + wallet.getDetails());
	        
	        String formattedAmount = NumberFormat.getInstance().format(wallet.getAmount());
	        amount.setText(formattedAmount + WalletConstants.SPACE 
	                + WalletConstants.CURRENCY_CODE);
	    return convertView;
	}	
}
